package cl.mt.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.LinkedList;

import cl.mt.dto.DTOPostulacion;
import cl.mt.util.bd.DatosTabla;
import cl.mt.util.bd.Parametro;

public class DataPostulacion extends DatosTabla {
	private static final String sqlGetId = "SELECT MAX(ID)+1 AS IDX FROM MT.MST_POSTULACIONES";
	private static final String sqlFindByPK = "SELECT ID, VHD_ID, OFC_ID, PRECIO_OFERTA, ELIMINADO, ANULADO, USR_ID, FECHA_PUBLICACION, FECHA_TERMINO, VHI_ID, MOT_ID, UNT_ID, CANTIDAD FROM MT.MST_POSTULACIONES where ID=?";
	private static final String sqlFindAll = "SELECT ID, VHD_ID, OFC_ID, PRECIO_OFERTA, ELIMINADO, ANULADO, USR_ID, FECHA_PUBLICACION, FECHA_TERMINO, VHI_ID, MOT_ID, UNT_ID, CANTIDAD FROM MT.MST_POSTULACIONES";
	private static final String sqlCreate = "INSERT INTO (ID, VHD_ID, OFC_ID, PRECIO_OFERTA, ELIMINADO, ANULADO, USR_ID, FECHA_PUBLICACION, FECHA_TERMINO, VHI_ID, MOT_ID, UNT_ID, CANTIDAD) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
	private static final String sqlDeleteByPK = "DELETE FROM MT.MST_POSTULACIONES where ID=?";


	private DTOPostulacion setDatosRegistro(ResultSet rs) throws SQLException {
		DTOPostulacion dto = new DTOPostulacion();
		dto.setId(formatInteger(rs.getObject("ID")));
		dto.setIdVehiculoDisponible(formatInteger(rs.getObject("VHD_ID")));
		dto.setIdOfertaCarga(formatInteger(rs.getObject("OFC_ID")));
		dto.setPrecioOferta(formatInteger(rs.getObject("PRECIO_OFERTA")));
		dto.setEliminado(formatString(rs.getObject("ELIMINADO")));
		dto.setAnulado(formatString(rs.getObject("ANULADO")));
		dto.setIdUsuario(formatInteger(rs.getObject("USR_ID")));
		dto.setFechaPublicaccion(formatDate(rs.getDate("FECHA_PUBLICACION")));
		dto.setFechaTermino(formatDate(rs.getDate("FECHA_TERMINO")));
		dto.setIdVehiculo(formatInteger(rs.getObject("VHI_ID")));
		dto.setIdTipoMoneda(formatInteger(rs.getObject("MOT_ID")));
		dto.setIdTipoUnidad(formatInteger(rs.getObject("UNT_ID")));
		dto.setCantidad(formatInteger(rs.getObject("CANTIDAD")));
		return dto;
	}

	public Integer generateId() {
		Parametro[] params = null;
		Integer id = null;
		try {
			getConnection();
			ResultSet rs = executeQuery(sqlGetId, params);
			while (rs.next()) {
				id = formatInteger(rs.getObject("IDX"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}
		return id;
	}
	public DTOPostulacion findByPrimaryKey(Long id) {

		DTOPostulacion dto = new DTOPostulacion();
		Parametro[] params = new Parametro[] { getParametro(id, Types.INTEGER)};
		try {
			getConnection();

			ResultSet rs = executeQuery(sqlFindByPK, params);
			while (rs.next()) {
				dto = setDatosRegistro( rs );
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return dto;
	}

	public Collection findAll() {

		Parametro[] params = null;
		Collection col = new LinkedList();
		try {
			getConnection();

			ResultSet rs = executeQuery(sqlFindAll, params);
			while (rs.next()) {
				DTOPostulacion dto = setDatosRegistro( rs );
				col.add( dto );
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return col;
	}

	public int deleteByPrimaryKey(Integer id) {

		Parametro[] params = new Parametro[] { getParametro(id, Types.INTEGER)};
		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlDeleteByPK, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return res;
	}

	public int create(DTOPostulacion dto) {

		Parametro[] params = new Parametro[] {
			getParametro(dto.getId(), Types.INTEGER),
			getParametro(dto.getIdVehiculoDisponible(), Types.INTEGER),
			getParametro(dto.getIdOfertaCarga(), Types.INTEGER),
			getParametro(dto.getPrecioOferta(), Types.INTEGER),
			getParametro(dto.getEliminado(), Types.VARCHAR),
			getParametro(dto.getAnulado(), Types.VARCHAR),
			getParametro(dto.getIdUsuario(), Types.INTEGER),
			getParametro(dto.getFechaPublicaccion(), Types.DATE),
			getParametro(dto.getFechaTermino(), Types.DATE),
			getParametro(dto.getIdVehiculo(), Types.INTEGER),
			getParametro(dto.getIdTipoMoneda(), Types.INTEGER),
			getParametro(dto.getIdTipoUnidad(), Types.INTEGER),
			getParametro(dto.getCantidad(), Types.INTEGER)
		};

		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlCreate, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return res;
	}



}

